# encoding: utf-8

import json
import pandas as pd
from pandas import Index

schema_list = [
    {"col": "_id", "title": "编号"},
    {"col": "brand", "title": "品牌"},
    {"col": "brandModel", "title": "型号"},
    {"col": "skuId", "title": "史泰博编码"},
    {"col": "skuName", "title": "史泰博名称"},
    {"col": "bindSkuName", "title": "绑定名称"},
    {"col": "bindSkuUrl", "title": "绑定链接"},
    # {"col": "originPlatSkuId", "title": "原平台编码"},
    {"col": "originPlatUrl", "title": "原平台链接"},

]

columns = list()
headers = list()

for meta in schema_list:
    columns.append(meta.get("col"))
    headers.append(meta.get("title"))


def _write_with_pandas(file, data_list, columns, headers):
    write = pd.ExcelWriter(file, engine="xlsxwriter", options={"strings_to_urls": False})
    df = pd.DataFrame(data_list)
    if not len(Index(columns) & df.columns) or len(Index(columns) & df.columns) != len(columns):
        miss_columns = list(set(columns) - set((Index(columns) & df.columns).tolist()))
        if miss_columns:
            for field in miss_columns:
                df.insert(loc=0, column=field, value="")
        df = df.reindex(columns=columns)

    df.to_excel(write, sheet_name="数据", columns=columns, header=headers, index=False)
    write.save()


file_path = "./可能错误的同款链接.xlsx"

with open("./maybe_err3.json", "r", encoding="utf-8") as f:
    datas = json.load(f)

data_list = []
filter_ = set()
for data in datas:
    _id = data.get("_id")
    data.pop("originPlatSkuId", None)
    if _id not in filter_:
        data_list.append(data)
        filter_.add(_id)

_write_with_pandas(file_path, data_list, columns, headers)
